import axios from 'axios'
import bodyPaser from 'body-parser'
import express from 'express'

axios.interceptors.response.use(res => {
    if (res.status === 200) {
        return res.data
    }
    return res
})

let medias = []

const search = () => {
    return new Promise((resolve, reject) => {
        axios({
            url: 'https://api.bilibili.com/x/v3/fav/resource/list',
            params: {
                media_id: '3470830542',
                pn: 1,
                ps: 36,
                keyword: '',
                order: 'mtime',
                tid: 0,
                platform: 'web',
                web_location: 333.1387
            }
        }).then((res) => {
            medias = res.data.medias
            resolve(res.data.medias)
        }).catch((err) => {
            reject(err)
        })
    })
}
search()

const app = express()
//应用bodyParser
app.use(bodyPaser.json())
app.use(
    bodyPaser.urlencoded({
        extended: false,
    })
)

app.get('/api/getVideos', (req, res) => {
    res.json(medias)
})

app.listen(9001, '0.0.0.0')

console.log('服务器启动:127.0.0.1:', 9001)